
-----------------------------------------------------------------------
-- Matrix.lua
--
-- A Framework for World Of Warcraft addons
-- Copyright (C) 2007  wwkkii
-- 
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-- 
-- Home Page: http://code.google.com/p/wowmatrix
-----------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Base Module
-------------------------------------------------------------------------------

MatrixBaseModule = MatrixClass("base", MatrixModule, MATRIX_NORMAL_CLASS, MatrixCommand, MatrixEvent);

function MatrixBaseModule:__call(...)
	self:ExecuteCommand(...);
end

function MatrixBaseModule:Initial()
	self:Super();
	self:InitialCommand();
	self:BindingSlash("matrix");
	self:RegisterCommand("test");
	self:RegisterCommand("show");
	self:RegisterCommand("meta");
	self:RegisterCommand("reload");
	self:RegisterEvent("PLAYER_LOGIN");
end

function MatrixBaseModule:Final()
	self:CleanupCommand();
	self:UnregisterAllEvents();
end

function MatrixBaseModule:OnCommandShow(arg)
	local item, value;

	value = nil;
	for item in string.gmatch(arg, "[a-zA-Z0-9_]+") do
		if (value == nil) then
			value = getglobal(item);
		else
			value = value[item];
		end
		if (item == nil) then
			matrix.Print("Value is nil.");
			return;
		end
	end
	matrix.ShowValue(arg, value, 3);
end

function MatrixBaseModule:OnCommandMeta(arg)
	local item, value;

	value = nil;
	for item in string.gmatch(arg, "[a-zA-Z0-9_]+") do
		if (value == nil) then
			value = getglobal(item);
		else
			value = value[item];
		end
		if (item == nil) then
			matrix.Print("Value is nil.");
			return;
		end
	end
	matrix.ShowValue(arg, getmetatable(value), 3);
end

function MatrixBaseModule:OnCommandTest(arg)
--	matrix.ShowValue(arg, getglobal(arg), 3);
--	matrix.Print(arg .."="..tostring(getfenv(a)));
--	matrix.Print("Here id = "..self:GetID());
--	matrix.ShowValue("matrix.error", matrix.GetException(), 3);
	local mt;
	mt = getmetatable(MatrixMainFrame);
	matrix.ShowValue(arg, getmetatable(mt), 3);
end

function MatrixBaseModule:OnCommandReload()
	ReloadUI();
end

function MatrixBaseModule:OnPlayerLogin()
	local idx, node, result, mesg;

	matrix_actived = 1;
	for idx = 1, matrix.GetModuleCount(), 1 do
		node = matrix.GetModule(idx);
		matrix.Print("Here active module: "..node:Name());
		node:SafeInvoke("OnActive");
		if (matrix.GetException() == nil) then
			node.actived = 1;
		end
	end
	matrix.Print("Active modules end.");
	for idx = 1, #MatrixApplet.__children__, 1 do
		node = MatrixApplet.__children__[idx];
		result, mesg = pcall(node.New, node);
		if (result) then
			matrix.SendEvent("APPLET_CREATE", mesg);
		else
			matrix.Error("Create applet "..node.__name__.." failed");
		end
	end
	for idx = 1, matrix.GetAppletCount(), 1 do
		node = matrix.GetApplet(idx);
		matrix.Print("Here active applet: "..node:Name());
		node:SafeInvoke("OnEnable");
		if (matrix.GetException() == nil) then
			node.actived = 1;
			matrix.SendEvent("APPLET_ENABLE", mesg);
		else
			matrix.Error("Enable Applet "..node:Name().." failed.");
		end
	end
	matrix.DumpStack(90);
end

MatrixBaseModule();

-------------------------------------------------------------------------------
-- MatrixTestApplet
-------------------------------------------------------------------------------

MatrixTestApplet = MatrixClass("test", MatrixApplet);

function MatrixTestApplet:Initial()
	self:Super();
	self:Print("Here create applet Tester.");
end

function MatrixTestApplet:OnEnable()
	self:Print("Here enable applet Tester.");
end

function MatrixTestApplet:OnDisable()
	self:Print("Here disable applet Tester.");
end

function MatrixTestApplet:OnReceve(mesg, sender)
end

-------------------------------------------------------------------------------
-- MatrixBaseOptions
-------------------------------------------------------------------------------

MatrixBaseOptions = MatrixDirOption("top",
	MatrixDirOption("parities",
		MatrixBoolOption("party1", false),
		MatrixBoolOption("party2", false),
		MatrixBoolOption("party3", false),
		MatrixBoolOption("party4", false),
		MatrixBoolOption("party5", false),
		MatrixBoolOption("party6", false),
		MatrixBoolOption("party7", false),
		MatrixBoolOption("party8", false)
	),
	MatrixDirOption("metiers",
		MatrixBoolOption("priest", false),
		MatrixBoolOption("hunter", false),
		MatrixBoolOption("warrior", false),
		MatrixBoolOption("mage", false),
		MatrixBoolOption("roge", false),
		MatrixBoolOption("druiy", false),
		MatrixBoolOption("party7", false),
		MatrixBoolOption("party8", false),
		MatrixToggleOption("week", 0xFF,
			"monday",
			"tuesday",
			"thursday",
			"wednesday",
			"friday",
			"saturday",
			"sunday")
	),
	MatrixNumOption("version", 1.23)
);

MatrixBaseOptions:Reset();
MatrixBaseOptions:Dump();

f = MatrixWidget:FromHandle(MatrixMainFrame);

f:SetFrameStrata("BACKGROUND")
f:SetWidth(128)  -- Set These to whatever height/width is needed 
f:SetHeight(64) -- for your Texture

f.t = f:CreateTexture(nil,"BACKGROUND")
f.t:SetTexture("Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Factions.blp")
f.t:SetAllPoints(f:Handle())

f:SetPoint("CENTER",0,0)
f:SetAlpha(0.5);
f:Show()
